﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Circle.Web.Application;
using Newtonsoft.Json.Serialization;
using Circle.Web.ViewModelsClient;
using Circle.Domain.Entity;
using Circle.Web.Application.Mvc;
using System.Web.Security;
using Circle.Manager;
using Circle.Domain.Model;
using Circle.Web.Drivers.Contract;

namespace Circle.Web.Controllers
{
   
    public class AccountController : BaseSiteMvcController
    {



        public ActionResult LoginOnPanel()
        {
            if (ControllerContext.IsChildAction || Request.IsAjaxRequest())
            {
                return View();
            }
            return HttpNotFound();
        }

        private void SaveTicketToCookie(TokenUser tokenUser, bool isPersistent)
        {
            
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, tokenUser.UserName, 
                                                    DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), isPersistent, tokenUser.Token);
            string hashticket = FormsAuthentication.Encrypt(ticket);
            HttpCookie usercookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashticket);
            Response.Cookies.Add(usercookie);
        }

        [HttpPost]
        public string Register(string jsonModel)
        {
            
            UserRegistrationClientModel registerUser = ClientModel.FromJson<UserRegistrationClientModel>(jsonModel);
            if (registerUser != null)
            {
                IAccountDriver driver = DependencyResolver.Current.GetService<IAccountDriver>();
                TokenUser tokenUser;
                OperationStatus status = driver.TryRegister(registerUser, out tokenUser);
                if (status == OperationStatus.RegisterSuccess)
                {
                    SaveTicketToCookie(tokenUser, false);
                    TokenUserClientModel clientTokenUser = new TokenUserClientModel(tokenUser);
                    return clientTokenUser.ToJson();
                }
            }


            return registerUser.ToJson();
        }

        [HttpPost]
        public string SignIn(string jsonModel)
        {
           
            UserSignInClientModel signInUser = ClientModel.FromJson<UserSignInClientModel>(jsonModel);
            if (signInUser != null)
            {
                try
                {
        
                    IAccountDriver driver = DependencyResolver.Current.GetService<IAccountDriver>();
                    TokenUser tokenUser;
                    OperationStatus status = driver.TrySignIn(signInUser, out tokenUser);
                    if (status == OperationStatus.SignInSuccess)
                    {
                        SaveTicketToCookie(tokenUser, signInUser.IsCookiePersistent);
                        TokenUserClientModel clientTokenUser = new TokenUserClientModel(tokenUser);

                        return clientTokenUser.ToJson();
                    }

                }
                catch (Exception ex)
                {
                    LoggingService.Error(ex);

                }
            }

            return signInUser.ToJson();
        }

        [HttpPost]
        public string SignOut(string jsonModel)
        {
            
            TokenUserClientModel tokenUser = ClientModel.FromJson<TokenUserClientModel>(jsonModel);
            if (tokenUser != null)
            {
                FormsAuthentication.SignOut();
               
            }
            return tokenUser.ToJson();

        }

    }
}
